package com.example.demo.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    // createOrUpdateUser: 检查是否存在openId，存在则不插入，否则插入
    @Override
    public void createOrUpdateUser(String openId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getOpenId, openId);

        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser == null) {
            User newUser = new User();
            newUser.setOpenId(openId);
            this.save(newUser);  // MyBatis Plus will handle ID automatically
        } else {
            System.out.println("该 openId 已经存在，不做插入操作。");
        }
    }
}